package com.ruoyi.patient_illness_info.service.impl;

import com.ruoyi.drug.domain.DrugInventory;
import java.util.List;
import java.util.Map;

import com.ruoyi.drug.mapper.DrugInventoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.patient_illness_info.mapper.PatientIllnessInfoMapper;
import com.ruoyi.patient_illness_info.domain.PatientIllnessInfo;
import com.ruoyi.patient_illness_info.service.IPatientIllnessInfoService;

/**
 * 患者患病信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-07
 */
@Service
public class PatientIllnessInfoServiceImpl implements IPatientIllnessInfoService 
{
    @Autowired
    private PatientIllnessInfoMapper patientIllnessInfoMapper;
    @Autowired
    private DrugInventoryMapper drugInventoryMapper;

    /**
     * 查询患者患病信息
     * 
     * @param patientId 患者患病信息主键
     * @return 患者患病信息
     */
    @Override
    public PatientIllnessInfo selectPatientIllnessInfoByPatientId(Long patientId)
    {
        return patientIllnessInfoMapper.selectPatientIllnessInfoByPatientId(patientId);
    }

    /**
     * 查询患者患病信息列表
     * 
     * @param patientIllnessInfo 患者患病信息
     * @return 患者患病信息
     */
    @Override
    public List<PatientIllnessInfo> selectPatientIllnessInfoList(PatientIllnessInfo patientIllnessInfo)
    {
        return patientIllnessInfoMapper.selectPatientIllnessInfoList(patientIllnessInfo);
    }
    @Override
    public List<Map<String,Long>> selectpatientcategorylist(String patient_idcard)
    {
        return patientIllnessInfoMapper.selectpatientcategorylist(patient_idcard);
    }



    @Override
    public int insertIllnessInfoAndreducedrug(PatientIllnessInfo patientIllnessInfo)
    {
        if (patientIllnessInfoMapper.insertPatientIllnessInfo(patientIllnessInfo)==1){
            Long medicationId = patientIllnessInfo.getMedicationId();//消耗药的编号
            Long dosage = patientIllnessInfo.getDosage();//拿药量
            DrugInventory drugInventory = drugInventoryMapper.selectDrugInventoryByDrugId(medicationId);//找到医疗资源表中的消耗药的对象
            Long stockQuantity = drugInventory.getStockQuantity();//获取库存量
            Long newstockQuantity=stockQuantity-dosage;//得到新的库存量
            DrugInventory drugInventory1 = new DrugInventory();
            drugInventory1.setDrugId(medicationId);
            drugInventory1.setStockQuantity(newstockQuantity);
            drugInventoryMapper.updateDrugInventory(drugInventory1);//更新医疗资源中的药品
            return 1;
        }
        else {
            return 0;
        }
    }

    /**
     * 修改患者患病信息
     * 
     * @param patientIllnessInfo 患者患病信息
     * @return 结果
     */
    @Override
    public int updatePatientIllnessInfo(PatientIllnessInfo patientIllnessInfo)
    {
        return patientIllnessInfoMapper.updatePatientIllnessInfo(patientIllnessInfo);
    }

    /**
     * 批量删除患者患病信息
     * 
     * @param patientIds 需要删除的患者患病信息主键
     * @return 结果
     */
    @Override
    public int deletePatientIllnessInfoByPatientIds(Long[] patientIds)
    {
        return patientIllnessInfoMapper.deletePatientIllnessInfoByPatientIds(patientIds);
    }

    /**
     * 删除患者患病信息信息
     * 
     * @param patientId 患者患病信息主键
     * @return 结果
     */
    @Override
    public int deletePatientIllnessInfoByPatientId(Long patientId)
    {
        return patientIllnessInfoMapper.deletePatientIllnessInfoByPatientId(patientId);
    }
}
